package cn.iocoder.yudao.module.resource.service.resource;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.resource.controller.admin.resource.vo.*;
import cn.iocoder.yudao.module.resource.dal.dataobject.resource.ResourceDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.resource.convert.resource.ResourceConvert;
import cn.iocoder.yudao.module.resource.dal.mysql.resource.ResourceMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.resource.enums.ErrorCodeConstants.*;

/**
 * 资源管理 - 视频 音频 字幕等 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ResourceServiceImpl implements ResourceService {

    @Resource
    private ResourceMapper resourceMapper;

    @Override
    public Long createResource(ResourceCreateReqVO createReqVO) {
        // 插入
        ResourceDO resource = ResourceConvert.INSTANCE.convert(createReqVO);
        resourceMapper.insert(resource);
        // 返回
        return resource.getId();
    }

    @Override
    public void updateResource(ResourceUpdateReqVO updateReqVO) {
        // 校验存在
        validateResourceExists(updateReqVO.getId());
        // 更新
        ResourceDO updateObj = ResourceConvert.INSTANCE.convert(updateReqVO);
        resourceMapper.updateById(updateObj);
    }

    @Override
    public void deleteResource(Long id) {
        // 校验存在
        validateResourceExists(id);
        // 删除
        resourceMapper.deleteById(id);
    }

    private void validateResourceExists(Long id) {
        if (resourceMapper.selectById(id) == null) {
            throw exception(RESOURCE_NOT_EXISTS);
        }
    }

    @Override
    public ResourceDO getResource(Long id) {
        return resourceMapper.selectById(id);
    }

    @Override
    public List<ResourceDO> getResourceList(Collection<Long> ids) {
        return resourceMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ResourceDO> getResourcePage(ResourcePageReqVO pageReqVO) {
        return resourceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ResourceDO> getResourceList(ResourceExportReqVO exportReqVO) {
        return resourceMapper.selectList(exportReqVO);
    }

}
